<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="个人信息">
      <van-icon
        slot="left"
        name="arrow-left"
        @click="
          $router.push({
            name: 'my',
            // 传递查询参数
            query: {
              redirect: '/my'
            }
          })
        "
      />
    </van-nav-bar>
    <!-- /导航栏 -->
    <input type="file" hidden accept="image/*" ref="file" @change="onFileChange" />
    <!-- 用户资料 -->
    <van-cell title="头像" is-link center @click="$refs.file.click()">
      <van-image width="30" height="30" round fit="cover" :src="user.photo" />
    </van-cell>
    <van-cell title="昵称" is-link :value="user.name" @click="isEditNameShow = true" />
    <van-cell title="性别" is-link :value="user.gender === 0 ? '男' : '女'" @click="isEditGenderShow = true" />
    <van-cell title="生日" is-link :value="user.birthday" @click="isEditBirthdayShow = true" />
    <!-- /用户资料 -->

    <!-- :user-name="user.name"
        @update-name="user.name = $event" -->

    <!-- v-model 只能使用一次-->

    <!-- 如果有多个数据需要保持同步，使用 .sync修饰符
            :gender.sync="user.gender"
             => :gender="user.gender"
                @update:gender="user.gender=$event"-->

    <!-- 修改昵称 -->
    <van-popup v-model="isEditNameShow" position="bottom" :style="{ height: '100%' }">
      <update-name v-if="isEditNameShow" v-model="user.name" @close="isEditNameShow = false" />
    </van-popup>
    <!-- /修改昵称 -->

    <!-- 修改性别 -->
    <van-popup v-model="isEditGenderShow" position="bottom">
      <update-gender v-if="isEditGenderShow" v-model="user.gender" @close="isEditGenderShow = false" />
    </van-popup>
    <!-- /修改性别 -->

    <!-- 修改生日 -->
    <van-popup v-model="isEditBirthdayShow" position="bottom">
      <update-birthday v-if="isEditBirthdayShow" v-model="user.birthday" @close="isEditBirthdayShow = false" />
    </van-popup>
    <!-- /修改生日 -->

    <!-- 修改头像 -->
    <van-popup class="vanPopup" v-model="isEditPhotoShow" position="bottom" :style="{ height: '100%' }">
      <update-photo
        v-if="isEditPhotoShow"
        :file="previewImage"
        @close="isEditPhotoShow = false"
        @update-photo="user.photo = $event"
      />
    </van-popup>
    <!-- /修改头像 -->
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthday from './components/update-birthday'
import UpdatePhoto from './components/update-photo'

export default {
  name: 'UserProfile',
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  props: {},
  data() {
    return {
      user: {},
      isEditNameShow: false, // 控制
      isEditGenderShow: false, //
      isEditBirthdayShow: false,
      isEditPhotoShow: false, //
      previewImage: null
    }
  },
  computed: {},
  watch: {},
  created() {
    this.loadUserProfile()
  },
  mounted() {},
  // 方法集合
  methods: {
    async loadUserProfile() {
      const { data } = await getUserProfile()
      this.user = data.data
    },
    onFileChange() {
      // 展示弹出层
      this.isEditPhotoShow = true
      // 在弹出层里面预览图片
      //   const blob = window.URL.createObjectURL(
      //     this.$refs.file.files[0]
      //   )
      this.previewImage = this.$refs.file.files[0]
      // 清空 fail 的value
      this.$refs.file.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.user-profile {
  .icon {
    font-size: 37px;
  }
  .van-image {
    box-sizing: border-box;
    margin-top: 10px;
  }
  .van-popup {
    background-color: #eeeeee;
  }
  .vanPopup {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #000;
  }
}
</style>
